package com.yunkaweilai.android.activity.operation.consumption.guadan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.operation.consumption.ReceivablesActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.PopupShopEmployee;
import com.yunkaweilai.android.model.guadan.GuadanCheckoutModel;
import com.yunkaweilai.android.model.operation.CreateSelectedGoodsModel;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.popup.GuadanShopEmployeePopup;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 挂单商品提成人
 * Created by Administrator on 2017/7/20.
 */

public class GuadanConfirmShopListActivity extends BaseActivity implements PopupShopEmployee {

    @BindView(R.id.id_listview)
    ListView idListview;
    @BindView(R.id.id_tv_all_price)
    TextView idTvAllPrice;
    @BindView(R.id.id_tv_guadan)
    TextView idTvGuadan;
    @BindView(R.id.id_tv_checkout)
    TextView idTvCheckout;
    @BindView(R.id.content_view)
    LinearLayout contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    public static final String GUADAN_CONFIRM_GID = "GUADAN_CONFIRM_GID";
    public static final String GUADAN_ROOM_PRICE = "GUADAN_ROOM_PRICE";
    public static final String GUADAN_TURN_TYPE = "GUADAN_TURN_TYPE";
    private String gid;
    private GuadanCheckoutModel checkoutModel;
    private float room_price = 0;
    private Gson gson = new Gson();
    private CreateSelectedGoodsModel createSelectedGoodsModel;//订单数据
    private CommonAdapter<CreateSelectedGoodsModel.DataBean.GoodsListBean> adapter;
    private ArrayList<CreateSelectedGoodsModel.DataBean.GoodsListBean> list = new ArrayList<>();
    private String type;

    public static void actionStart(Context context,String gid,String room_price,String type){
        Intent intent = new Intent(context,GuadanConfirmShopListActivity.class);
        intent.putExtra(GUADAN_CONFIRM_GID,gid);
        intent.putExtra(GUADAN_ROOM_PRICE,room_price);
        intent.putExtra(GUADAN_TURN_TYPE,type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_shop_list);

        ButterKnife.bind(this);
        type = getIntent().getStringExtra(GUADAN_TURN_TYPE);
        gid = getIntent().getStringExtra(GUADAN_CONFIRM_GID);
        room_price = Float.parseFloat(getIntent().getStringExtra(GUADAN_ROOM_PRICE));
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        new TitleBuilder(mAc).setTitleText("设置提成人")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        idTvGuadan.setVisibility(View.GONE);
        initAdapter();
        initHttp();
        initClick();
    }


    private void initClick() {
        idTvCheckout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() > 0){
                    GuadanReceivablesActivity.actionStart(mAc, idTvAllPrice.getText().toString() + "", createrStr2(), Api.CREATE_GUADAN_ORDER,gid,type,room_price+"");
                }else{
                    showToast("还未选择商品");
                }

            }
        });
    }
    //确认商品后添加提成人
    private String createrStr2(){
        String strs = "";
        for (int i = 0; i < createSelectedGoodsModel.getData().getGoodsList().size(); i++) {
            CreateSelectedGoodsModel.DataBean.GoodsListBean goodsListBean = createSelectedGoodsModel.getData().getGoodsList().get(i);
            strs = strs + goodsListBean.getGoods_id() + "|" + goodsListBean.getGoods_num() + "|" + goodsListBean.getGoods_type();
            if (!StringUtils.isEmpty(goodsListBean.getEmplyeeStr())){
                strs = strs + "|" + goodsListBean.getEmplyeeStr() + ",";
            }else{
                strs = strs + ",";
            }
        }
        if (strs.length() > 0){
            strs = strs.substring(0, strs.length() - 1);
        }
        return strs;
    }

    //通过订单号获取商品字符串
    private void initHttp() {
        HttpRequest.postUrl(Api.GUADAN_CHECKOUT_INFO)
                .addParams("gid",gid)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            checkoutModel = gson.fromJson(json, GuadanCheckoutModel.class);
                            initGoodList();
                        }else{
                            if (idMultipleStatusView != null){
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null){
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    //获取商品列表
    private void initGoodList(){
        HttpRequest.postUrl(Api.CREATE_SELECTED_GOODS)
                .addParams("SelectedGoodsStr", checkoutModel.getData().getSelectedGoodsStr())
                .addParams("FangFei",room_price +"")
                .addParams("gid",gid)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            createSelectedGoodsModel = gson.fromJson(json, CreateSelectedGoodsModel.class);
                            list.clear();
                            if (room_price > 0){
                                CreateSelectedGoodsModel.DataBean.GoodsListBean goodsListBean = new CreateSelectedGoodsModel.DataBean.GoodsListBean();
                                goodsListBean.setGoods_image("");
                                goodsListBean.setGoods_name("开台费");
                                goodsListBean.setShop_discount_price(Utils.ManageMoney(room_price+""));
                                goodsListBean.setFlag(true);
                                list.add(goodsListBean);
                            }
                            list.addAll(createSelectedGoodsModel.getData().getGoodsList());
                            adapter.notifyDataSetChanged();
                            idTvAllPrice.setText(createSelectedGoodsModel.getData().getOrderInfo().getPayment() +"");
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showContent();
                            }
                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    private void initAdapter() {
        adapter = new CommonAdapter<CreateSelectedGoodsModel.DataBean.GoodsListBean>(mAc, R.layout.item_list_guadan_shop_consumption, list) {
            @Override
            protected void convert(ViewHolder viewHolder, final CreateSelectedGoodsModel.DataBean.GoodsListBean item, final int position) {
                ImageView mIdImg = viewHolder.getView(R.id.id_img);

                viewHolder.setText(R.id.id_tv_name, item.getGoods_name() + "");
                TextView idTvCommission = viewHolder.getView(R.id.id_tv_commission);

                if (item.isFlag()){
                    viewHolder.setText(R.id.id_tv_info, Utils.ManageMoney(item.getShop_discount_price())+"");
                    idTvCommission.setVisibility(View.GONE);
                    GlideUtils.loadImage(mAc,R.mipmap.ic_fangtaifei,mIdImg);
                }else{
                    viewHolder.setText(R.id.id_tv_info, Utils.ManageMoney(item.getShop_discount_price()) + "/" + item.getUnit() + " X" + item.getGoods_num());
                    idTvCommission.setVisibility(View.VISIBLE);
                    Utils.shopInfoImg(mAc,item.getGoods_image(),mIdImg);

                }

                if (StringUtils.isEmpty(item.getEmplyeeName())) {
                    idTvCommission.setText("提成员工");
                } else {

                    idTvCommission.setText(item.getEmplyeeName());
                }
                idTvCommission.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String employeeStr = "";
                        if (!StringUtils.isEmpty(item.getEmplyeeStr())){
                            employeeStr = item.getEmplyeeStr();
                        }
                        GuadanShopEmployeePopup shopPopup = new GuadanShopEmployeePopup(mAc, item.getGoods_id(), employeeStr,item.getGoods_type(),item);
                        shopPopup.setInterface(GuadanConfirmShopListActivity.this);
                        shopPopup.showPopupWindow();
                    }
                });

            }
        };
        idListview.setAdapter(adapter);
    }


    @Override
    public void refresh() {
        for (int i = 0; i < list.size(); i++) {
            CreateSelectedGoodsModel.DataBean.GoodsListBean goodsListBean = list.get(i);
            showLog(goodsListBean.getEmplyeeName() + ":"  + goodsListBean.getEmplyeeStr());
        }
        adapter.notifyDataSetChanged();
    }
}
